package com.service.impl;

import com.dao.impl.BookDaoImpl;
import com.javabean.Book;
import com.javabean.Page;
import com.service.BookService;


import java.util.List;

public class BookServiceImpl implements BookService {
    @Override
    public void addBook(Book book) {
        new BookDaoImpl().addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
        new BookDaoImpl().deleteBookById(id);

    }

    @Override
    public void updateBook(Book book) {

        new BookDaoImpl().updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {

        return new BookDaoImpl().queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() {
        return new BookDaoImpl().queryBooks();
    }

    @Override
    public Page<Book> page(Integer pageNum, Integer pageSize) {
        Page<Book> bookPage = new Page<>();
        //设置当前页
        bookPage.setPageNum(pageNum);
        //设置每页显示的数量
        bookPage.setPageSize(pageSize);
        Integer pageTotalCount = new BookDaoImpl().queryForPageTotalCount();
        //设置总记录数
        bookPage.setPageTotalCount(pageTotalCount);
        //设置总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal = pageTotal + 1;
        }
        bookPage.setPageTotal(pageTotal);
        //设置当前页数据
        List<Book> items = new BookDaoImpl().queryForPageItems((pageNum - 1) * pageSize, pageSize);
        bookPage.setItems(items);
        return bookPage;
    }

    @Override
    public Page<Book> pageByPrice(int pageNum, int pageSize, int min, int max) {
        Page<Book> bookPage = new Page<>();
        //设置当前页
        bookPage.setPageNum(pageNum);
        //设置每页显示的数量
        bookPage.setPageSize(pageSize);
        //设置价格区间总记录数
        Integer pageTotalCountByPrice = new BookDaoImpl().queryForPageTotalCountByPrice(min, max);
        bookPage.setPageTotalCount(pageTotalCountByPrice);
        //设置总页码
        Integer pageTotalByPrice = pageTotalCountByPrice / pageSize;
        if (pageTotalCountByPrice % pageSize > 0) {
            pageTotalByPrice = pageTotalByPrice + 1;
        }
        bookPage.setPageTotal(pageTotalByPrice);
        //设置当前页数据
        List<Book> items = new BookDaoImpl().queryForPageItemsByPrice((pageNum - 1) * pageSize, pageSize, min, max);
        bookPage.setItems(items);
        return bookPage;
    }
}
